<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Associates a key with a value</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-map.pairs.html">« Ds\Map::pairs</a></li>
      <li style="float: right;"><a href="ds-map.putall.html">Ds\Map::putAll »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.ds-map.html">Map</a></li>
    <li>Associates a key with a value</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ds-map.put" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Ds\Map::put</h1>
  <p class="verinfo">(PECL ds &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Map::put</span> &mdash; <span class="dc-title">Associates a key with a value</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-ds-map.put-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Ds\Map::put</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
    Associates a <code class="parameter">key</code> with a <code class="parameter">value</code>,
    overwriting a previous association if one exists.
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
        Keys of type <span class="type">object</span> are supported.

        If an object implements <span class="classname"><strong class="classname">Ds\Hashable</strong></span>,
        equality will be determined by the object&#039;s <code class="code">equals</code> function.

        If an object does not implement <span class="classname"><strong class="classname">Ds\Hashable</strong></span>,
        objects must be references to the same instance to be considered equal.
    </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
        You can also use array syntax to associate values by key, eg. <code class="code">$map[&quot;key&quot;] = $value</code>.
    </p>
  </p></blockquote>

  <div class="caution"><strong class="caution">警告</strong>
    <p class="para">
        Be careful when using array syntax. Scalar keys will be coerced to
        integers by the engine. For example, <code class="code">$map[&quot;1&quot;]</code> will attempt
        to access <code class="code">int(1)</code>, while <code class="code">$map-&gt;get(&quot;1&quot;)</code> will
        correctly look up the string key.
    </p>
    <p class="para">
        See <a href="language.types.array.html" class="link">Arrays</a>.
    </p>
  </div>

 </div>


 <div class="refsect1 parameters" id="refsect1-ds-map.put-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">key</code></dt>

    <dd>

     <p class="para">
        The key to associate the value with.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">value</code></dt>

    <dd>

     <p class="para">
        The value to be associated with the key.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-ds-map.put-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-ds-map.put-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4098">
   <p><strong>示例 #1 <span class="function"><strong>Ds\Map::put()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$map&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Map</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$map</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Ds\Map Object
(
    [0] =&gt; Ds\Pair Object
        (
            [key] =&gt; a
            [value] =&gt; 1
        )

    [1] =&gt; Ds\Pair Object
        (
            [key] =&gt; b
            [value] =&gt; 2
        )

    [2] =&gt; Ds\Pair Object
        (
            [key] =&gt; c
            [value] =&gt; 3
        )

)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4099">
   <p><strong>示例 #2 <span class="function"><strong>Ds\Map::put()</strong></span> example using objects as keys</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">HashableObject&nbsp;</span><span style="color: #007700">implements&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Hashable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;An&nbsp;arbitrary&nbsp;value&nbsp;to&nbsp;use&nbsp;as&nbsp;the&nbsp;hash&nbsp;value.&nbsp;Does&nbsp;not&nbsp;define&nbsp;equality.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">private&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">hash</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">equals</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">bool<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$map&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Map</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">([]);<br /><br /></span><span style="color: #FF8000">//&nbsp;Using&nbsp;the&nbsp;same&nbsp;instance&nbsp;multiple&nbsp;times&nbsp;will&nbsp;overwrite&nbsp;the&nbsp;previous&nbsp;value.<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Using&nbsp;multiple&nbsp;instances&nbsp;of&nbsp;the&nbsp;same&nbsp;object&nbsp;will&nbsp;create&nbsp;new&nbsp;associations.<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Using&nbsp;multiple&nbsp;instances&nbsp;of&nbsp;equal&nbsp;hashable&nbsp;objects&nbsp;will&nbsp;overwrite&nbsp;previous&nbsp;values.<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">HashableObject</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">HashableObject</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">HashableObject</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">put</span><span style="color: #007700">(new&nbsp;\</span><span style="color: #0000BB">HashableObject</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$map</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(Ds\Map)#1 (5) {
  [0]=&gt;
  object(Ds\Pair)#7 (2) {
    [&quot;key&quot;]=&gt;
    object(ArrayIterator)#2 (1) {
      [&quot;storage&quot;:&quot;ArrayIterator&quot;:private]=&gt;
      array(0) {
      }
    }
    [&quot;value&quot;]=&gt;
    int(2)
  }
  [1]=&gt;
  object(Ds\Pair)#8 (2) {
    [&quot;key&quot;]=&gt;
    object(stdClass)#3 (0) {
    }
    [&quot;value&quot;]=&gt;
    int(3)
  }
  [2]=&gt;
  object(Ds\Pair)#9 (2) {
    [&quot;key&quot;]=&gt;
    object(stdClass)#4 (0) {
    }
    [&quot;value&quot;]=&gt;
    int(4)
  }
  [3]=&gt;
  object(Ds\Pair)#10 (2) {
    [&quot;key&quot;]=&gt;
    object(HashableObject)#5 (1) {
      [&quot;value&quot;:&quot;HashableObject&quot;:private]=&gt;
      int(1)
    }
    [&quot;value&quot;]=&gt;
    int(6)
  }
  [4]=&gt;
  object(Ds\Pair)#11 (2) {
    [&quot;key&quot;]=&gt;
    object(HashableObject)#6 (1) {
      [&quot;value&quot;:&quot;HashableObject&quot;:private]=&gt;
      int(2)
    }
    [&quot;value&quot;]=&gt;
    int(8)
  }
}
</pre></div>
   </div>
  </div>
 </div>



</div></div></div></body></html>